package com.yxzr.youyou.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.yxzr.youyou.R;
import com.yxzr.youyou.bean.ExchangeRecordBean;
import com.yxzr.youyou.retrofit.DataUrl;
import com.yxzr.youyou.utils.DateTimeUtil;

import java.util.ArrayList;
import java.util.List;

public class ExchangeRecordAdapter extends RecyclerView.Adapter<ExchangeRecordAdapter.ViewHolder>  {
    private Context mContext;
    private List<ExchangeRecordBean.DataBean> list = new ArrayList<>();
    public ExchangeRecordAdapter(Context context){
        this.mContext = context;
    }
    public void addList(List<ExchangeRecordBean.DataBean> mlist){
        if (mlist != null && mlist.size() > 0){
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.exchangerecord_adapter_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(mContext)
                .setDefaultRequestOptions(
                        new RequestOptions()
                                .frame(1000000)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .centerCrop()
                                .error(R.drawable.dialog_loading)
                                .placeholder(R.drawable.dialog_loading))
                .load(DataUrl.imgUrl + list.get(position).getPath())
                .into(holder.integrallmall_iv);

        holder.integrallmall_tv_desc.setText(list.get(position).getDay()+"天VIP");
        holder.integrallmall_tv_num.setText("您于"+ DateTimeUtil.formatDateTime((Long.parseLong(list.get(position).getCreate_time()) * 1000),"yyyy/MM/dd")+
                "兑换"+list.get(position).getDay()+"天VIP"+list.get(position).getNum()+"个");
        holder.integrallmall_btn.setText(list.get(position).getIntegral()+"积分兑换");

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView integrallmall_iv;
        private TextView integrallmall_tv_desc,integrallmall_tv_num;
        private Button integrallmall_btn;
        public ViewHolder(View itemView) {
            super(itemView);
            integrallmall_iv = itemView.findViewById(R.id.integrallmall_iv);
            integrallmall_tv_desc = itemView.findViewById(R.id.integrallmall_tv_desc);
            integrallmall_tv_num = itemView.findViewById(R.id.integrallmall_tv_num);
            integrallmall_btn = itemView.findViewById(R.id.integrallmall_btn);
        }
    }
}
